import sys
from datetime import datetime

from PyQt5.QtCore import QCoreApplication
#from PyQt5.QtCore import QThread

from Group import Ui_Swindow as Ui_Dialog
from spider_main import *
from save_mysql import ReadMySQL
#import PyQt5.QtWidgets.QMainWindow
from PyQt5.QtWidgets import QApplication, QMainWindow
import os
from login_save_cookies import save_cookies
import logging


class mywindow(QMainWindow, Ui_Dialog):
    def __init__(self):
        super(mywindow, self).__init__()
        self.setupUi(self)
        self.run_spider_connect()

    def run_spider_connect(self):
        # 启动采集程序
        self.pushButton.clicked.connect(self.run_spider)
        self.pushButton_2.clicked.connect(QCoreApplication.instance().quit)
        self.pushButton_3.clicked.connect(self.open_log)
        self.pushButton_4.clicked.connect(self.open_login)
        self.pushButton_clear_all.clicked.connect(self.clear_all_table)

    def printf(self, mypstr):
        self.textBrowser.append(mypstr)  # 在指定的区域显示提示信息
        self.cursot = self.textBrowser.textCursor()
        self.textBrowser.moveCursor(self.cursot.End)  # 光标移到最后，这样就会自动显示出来

   #启动采集程序
    def run_spider(self):
        try:
            logging.basicConfig(level=20,filename='./采集日志.log',format=('%(asctime)s - %(module)s.%(funcName)s.%(lineno)d - %(levelname)s - %(message)s'))
            cookies = get_cookies_decode_to_dict()
            if cookies==None:
                self.printf(f"cookies.txt不存在，请先模拟登录。")
                logging.info(f"cookies.txt不存在，请先模拟登录。")
                return
            read = ReadMySQL()
            data = read.read_data()   #读取源数据库表
            print(data)
            if data is None or data ==():
                self.printf(f"源公司名data不存在，请先导入原始数据!")
                logging.info(f"源公司名data不存在，请先导入原始数据!")
                return
            else:
                for value in data:
                    key = value[1]  #取公司名称
                    # if key == '-' or key is None:
                    #     key = value[1]
                    s = Spider(cookies, value=key)
                    a, b = s.run()   #工商注册号，公司名称
                    QApplication.processEvents()
                    if b=='没有工商注册号':
                        self.printf(f"{value[1]} 没有工商注册号，跳过采集。")
                        logging.info(f"{value[1]} 没有工商注册号，跳过采集。")
                        continue
                    if b == 'cookies过期请重新登录':
                        self.printf("采集停止，cookies过期，请重新登录。")
                        logging.info(f"采集停止，cookies过期，请重新登录。")
                        break
                    if b == '需要手工登录网页进行验证':
                        self.printf("采集停止，请手工登录网页进行验证，再开启采集程序。")
                        logging.info("采集停止，请手工登录网页进行验证，再开启采集程序。")
                        break
                    if a != None:
                        self.printf(f'{value[1]} {b} 采集失败，错误信息：{a}' )
                        logging.info(f"{a}    {value[1]} {b} 采集失败")
                        continue
                    c = read.read_already_update()[0][0]
                    if c == 0:
                        c = 1
                    self.printf(f"{value[1]}  采集成功，已存入{c}条数据")

            # 失信人生成excel
            res = read.query_data()
            b = to_list(res)
            fileds = [["name", "iname", "shixin", "casecode", "province", "publishdate", "regdate", "businessentity",
                       "disrupttype name",
                       "cardnum", "courtname", "courtarea", "sexy", "gistunit", "performance", "duty", "gistId"]]  # 列名
            title = '失信人'
            save_excel(title, fileds, b)
            self.printf(f"成功导出失信人数据！")

            # 高管生成excel
            # res = read.query_gg_data()
            # # print(res)
            # b = to_list(res)
            # fileds = [["name", "iname", "post"]]  # 列名
            # title = '高管'
            # save_excel(title, fileds, b)
            # self.printf(f"成功导出失信人公司高管数据！")

            self.printf(f"数据库已全部采集完成。")

        except Exception  as e:
            self.printf(e +",程序异常退出!")
            logging.info(e +",程序异常退出")

    def open_log(self):
        path=os.path.abspath('./采集日志.log')
        os.system("start explorer %s" % path)

    # 清空数据
    def clear_all_table(self):
        self.pushButton_clear_all.setEnabled(False)
        read = ReadMySQL()
        s_db = read.clera_all_table()
        #s_db = self.clera_all_table()
        if s_db == 0:
            #self.textEdit_log.append('已清空所有数据,剩余数据条数：{}'.format(s_db))
            #self.printf("采集停止，请手工登录网页进行验证，再开启采集程序。")
            self.printf("已清空数据库所有数据!")
            logging.info("已清空数据库所有数据。")
        else:
            #self.textEdit_log.append('数据清空失败,错误信息：{}'.format(s_db))
            self.printf("数据清空失败!")
            logging.info("数据清空失败。")
        self.pushButton_clear_all.setEnabled(True)

    def open_login(self):
        save_cookies()
        QApplication.processEvents()

from datetime import datetime
def deadlines(year, mouth, day):
    now = datetime.now().timestamp()
    expire = datetime(year, mouth, day, 0, 0).timestamp()
    res = expire - now
    if res < 0:
        raise Exception

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window_app = mywindow()
    deadlines(2020, 12, 29)
    window_app.setWindowTitle("天眼查数据采集")
    window_app.show()
    sys.exit(app.exec_())
